import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();


console.log(1);

class DataSoruce {
  constructor(){
    //订阅者
    this.lisener = []
  }

  //订阅
  emit(callback){
    this.lisener.push(callback)
  }

  //发布
  on(data){
    //遍历订阅者
    for(let i=0;i<this.lisener.length;i++){
      const lisener = this.lisener[i]
      lisener(data)
    }
  }

  //取消订阅
  off(callback){
    var index = this.lisener.indexOf(callback)
    this.lisener.splice(index,1)
  }
}

const data_source = new DataSoruce
data_source.emit((data)=>{
  console.log('获取订阅',data);
  
})

data_source.on('fabudingyue')
data_source.off((data)=>{
  console.log('删除订阅',data);
})
data_source.on('fabudingyue')

